<?php

namespace Goods\Controller;
use Common\Controller\HomebaseController;

class ListController extends HomebaseController {

	// 前台商品列表
	public function index() {
	    $id=I('get.id',0,'intval');
		$category=sp_get_term($id);

		$all_catid = sp_get_all_catid($id); //获取所有子类
								
		if(empty($category)){
		    header('HTTP/1.1 404 Not Found');
		    header('Status:404 Not Found');
		    if(sp_template_file_exists(MODULE_NAME."/404")){
		        $this->display(":404");
		    }
		    return;
		}
		
		$crumbs = sp_get_breadcrumb($id);
		
		$sort = $_GET['order'] == 'desc' ? 'asc' : 'desc';
		
		if($sort == 'desc' && isset($_GET['order'])){
			$sort_icon = '<i class="fa fa-long-arrow-up"></i>';
		}else if($sort == 'asc' && isset($_GET['order'])){
			$sort_icon = '<i class="fa fa-long-arrow-down"></i>';
		}
		
		if(isset($_GET['sort']) && isset($_GET['order'])){			
			$order = "{$_GET['sort']} {$_GET['order']},on_time DESC";
		}
		
		if(empty($all_catid)){
			$where=array("cat_id"=>$id,"is_on_sale"=>1,"is_delete"=>"neq 3");
		}else{
			$where=array("cat_id"=>array('in',implode(',',$all_catid)),"is_on_sale"=>1,"is_delete"=>"neq 3");
		}
		
		$count = M('Goods')->where($where)->count();
		$page = $this->page($count, 12);
		$list = M('Goods')
		->where($where)
		->limit($page->firstRow , $page->listRows)
		->order($order)
		->select();
		
		
		$tplname=$category["list_tpl"];
    	$tplname=sp_get_apphome_tpl($tplname, "list");
		
    	
    	$this->assign('crumbs', $crumbs);    	
    	$this->assign('category', $category['name']);
		$this->assign('list', $list);
		$this->assign('page', $page->show('Home'));
		$this->assign('cat_id', $id);
		$this->assign('sort', $sort);
		$this->assign('sort_icon', $sort_icon);
    	$this->display(":$tplname");
	}
	
	
	// 用于后台导航编辑添加
	public function nav_index(){
		$navcatname="商品分类";
        $category_obj= M("Goods_category");

        $where=array();
        $where['is_show'] = array('eq',1);
        $categorys=$category_obj->field('id,name,parent_id')->where($where)->order('id')->select();
		$datas=$categorys;
		$navrule = array(
		    "id"=>'id',
            "action" => "Goods/List/index",
            "param" => array(
                "id" => "id"
            ),
            "label" => "name",
		    "parentid"=>'parent_id'
        );
		return sp_get_nav4admin($navcatname,$datas,$navrule) ;
	}
}
